package com.hongxun.modules.api.rest;

import com.alibaba.fastjson.JSON;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;
import com.hongxun.modules.bss.entity.DPictureEntity;
import com.hongxun.modules.bss.service.DPictureService;
import com.hongxun.modules.csg.entity.*;
import com.hongxun.modules.csg.service.*;
import com.sun.source.tree.LabeledStatementTree;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.util.TextUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


/**
 * 中间检查
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-30 10:08:43
 */
@RestController
@RequestMapping("/rest/cmiddlecheck")
public class CMiddleCheckRestController {
	@Autowired
	CMiddleCheckService cMiddleCheckService;
	@Autowired
	CBasicsWorkService cBasicsWorkService;
	@Autowired
	CGroundService cGroundService;
	@Autowired
	CLinePitService cLinePitService;
	@Autowired
	CPolePitService cPolePitService;
	@Autowired
	private CPBaseService cPBaseService;


	/**
	 * 	判断是否归档或者是否已删除
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping("/isArchives")
	public R isArchives(@RequestParam Map<String, Object> params){
		String pGuid = params.get("pGuid").toString();
		CPBaseEntity cpBase = cPBaseService.queryObject(pGuid);
		if (cpBase.getPBState().equals("30")) {
			return R.error(11,"该项目已归档");
		}else {
			if (cpBase.getPBDatastate().equals("删除")) {
				return R.error(22,"该项目已删除");
			}else {
				return R.ok();
			}
		}

	}


	/**
	 * 获取信息
	 */
	@RequestMapping("/getInfo")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		String pGuid = params.get("pGuid").toString();


		//1.采集记录
		Map<String , Object> cMiddleCheckMap = new HashMap<>();
		String whereCMiddleCheck = " 1=1 and p_guid='"+pGuid+"'";
		cMiddleCheckMap.put("filterSql",whereCMiddleCheck+" ");
		List<CMiddleCheckEntity> cMiddleCheckEntityList = cMiddleCheckService.queryList(cMiddleCheckMap);

		//2.设备基础
		Map<String , Object> CBasicsWorkMap = new HashMap<>();
		String whereCBasicsWork = " 1=1 and p_guid='"+pGuid+"'";
		CBasicsWorkMap.put("filterSql",whereCBasicsWork);
		List<CBasicsWorkEntity> cBasicsWorkEntityList = cBasicsWorkService.queryList(CBasicsWorkMap);


		//3.接地体
		Map<String , Object> CGroundMap = new HashMap<>();
		String whereCGround = " 1=1 and p_guid='"+pGuid+"'";
		CGroundMap.put("filterSql",whereCGround);
		List<CGroundEntity> cGroundEntityList = cGroundService.queryList(CGroundMap);

		//4.拉线坑
		Map<String , Object> CLinePitMap = new HashMap<>();
		String whereCLinePit = " 1=1 and p_guid='"+pGuid+"'";
		CLinePitMap.put("filterSql",whereCLinePit);
		List<CLinePitEntity> cLinePitEntityList = cLinePitService.queryList(CLinePitMap);

		//5.杆塔坑
		Map<String , Object> CPolePitMap = new HashMap<>();
		String whereCPolePit = " 1=1 and p_guid='"+pGuid+"'";
		CPolePitMap.put("filterSql",whereCPolePit);
		List<CPolePitEntity> cPolePitEntityList = cPolePitService.queryList(CPolePitMap);


       return R.ok().put("cMiddleCheckEntityList",JSON.toJSONString(cMiddleCheckEntityList))
			   		.put("cBasicsWorkEntityList",JSON.toJSONString(cBasicsWorkEntityList))
			   		.put("cGroundEntityList",JSON.toJSONString(cGroundEntityList))
			   		.put("cLinePitEntityList",JSON.toJSONString(cLinePitEntityList))
			   		.put("cPolePitEntityList",JSON.toJSONString(cPolePitEntityList));
	}


	/**
	 * 同步
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping("/addCMiddleCheck")
	public R addCMiddleCheck(@RequestParam Map<String, Object> params){
		//查询列表数据
		//1.采集记录
		String CMiddleCheck = params.get("cMiddleCheckEntityList").toString();
		List<CMiddleCheckEntity> cMiddleCheckEntityList = JSON.parseArray(CMiddleCheck,CMiddleCheckEntity.class);
		if (cMiddleCheckEntityList != null) {
			cMiddleCheckService.insertOrReplaceOrDelete(cMiddleCheckEntityList);
		}

		//2.设备基础
		String CBasicsWork = params.get("cBasicsWorkEntityList").toString();
		List<CBasicsWorkEntity> cBasicsWorkEntityList = JSON.parseArray(CBasicsWork , CBasicsWorkEntity.class);
		if (cBasicsWorkEntityList != null) {
			cBasicsWorkService.insertOrReplaceOrDelete(cBasicsWorkEntityList);
		}

		//3.接地体
		String CGround = params.get("cGroundEntityList").toString();
		List<CGroundEntity> cGroundEntityList = JSON.parseArray(CGround,CGroundEntity.class);
		if (cGroundEntityList != null) {
			cGroundService.insertOrReplaceOrDelete(cGroundEntityList);
		}

		//4.拉线坑
		String CLinePit = params.get("cLinePitEntityList").toString();
		List<CLinePitEntity> cLinePitEntityList = JSON.parseArray(CLinePit,CLinePitEntity.class);
		if (cLinePitEntityList != null) {
			cLinePitService.insertOrReplaceOrDelete(cLinePitEntityList);
		}

		//5.杆塔坑
		String CPolePit = params.get("cPolePitEntityList").toString();
		List<CPolePitEntity> cPolePitEntityList = JSON.parseArray(CPolePit,CPolePitEntity.class);
		if (cPolePitEntityList != null) {
			cPolePitService.insertOrReplaceOrDelete(cPolePitEntityList);
		}

		return R.ok();
	}


}
